//
//  TestFunButton.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2022/2/22.
//  Copyright © 2022 moon. All rights reserved.
//

#import "TestFunButton.h"

@implementation TestFunButton
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
        self.contentInsets = MKUIEdgeInsetsMakeSameEdge(10);
        self.minHitSize = CGSizeMake(30, 50);
        self.layer.borderColor = [UIColor systemGrayColor].CGColor;
        self.layer.borderWidth = 1/[UIScreen mainScreen].scale;
        self.layer.cornerRadius = 3;
    }
    return self;
}
- (id)initWithTitle:(NSString *)title{
    if (self=[self initWithContentStyle:(MKUIFlowLayoutButtonContentStyleHorizontal)]) {
        [self setTitle:title forState:(UIControlStateNormal)];
    }
    return self;
}
+ (id)buttonWithTitle:(NSString *)title click:(void(^)(TestFunButton *button))click{
    TestFunButton *button = [[self alloc] initWithTitle:title];
    [button mk_addClickActionBlock:^(TestFunButton *self) {
        if(click){
            click(self);
        }
    } context:self];
    return button;
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [super sizeThatFits:size];
    s.width = MAX(s.width,44);
    s.height = MAX(s.height,30);
    return s;
}
@end

@interface TestFunButtons()
@property(nonatomic,strong) MKUIWaterFlowLayoutConstraint *flowlayout;
@end

@implementation TestFunButtons
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.flowlayout = [[MKUIWaterFlowLayoutConstraint alloc] initWithItems:@[] constraintParam:(MKUIWaterFlowLayoutConstraintParam_H_C_L) contentInsets:MKUIEdgeInsetsMakeSameEdge(5) interitemSpacing:5 lineSpacing:5];
    }
    return self;
}
- (void)addButton:(NSString *)title click:(void(^)(TestFunButtons *buttons))block{
    TestFunButton *button = [[TestFunButton alloc] initWithTitle:title];
    [button mk_addClickActionBlock:^(TestFunButtons *self) {
        if(block){
            block(self);
        }
    } context:self];
    [self addSubview:button];
    [self.flowlayout addItem:button];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return [self.flowlayout sizeThatFits:size resizeItems:YES];
}
@end
